﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;


public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.Form.DefaultFocus = txtUserName.ClientID;
            Page.Form.DefaultButton = cmdLogin.UniqueID;
        }

    }
    protected void cmdLogin_Click(object sender, EventArgs e)
    {
        

        //check for valid page
        if (!Page.IsValid) { return; }

        //Clear session for new user
        //Session.Clear();

        LookupAcct();
    }

    protected void cmdCrtAcct_Click(object sender, EventArgs e)
    {
        loginRequiredFieldValidator.Enabled = false;
        passRequiredFieldValidator.Enabled = false;
        //check for valid page
        if (!Page.IsValid) { return; }

        Response.Redirect("AccountManagement.aspx");
    }

    private void LookupAcct()
    {
        OleDbConnection objConnection;
        OleDbDataAdapter objCommand;
        String strConnect;
        String strCommand;
        String acctType;
        DataTable dt;

        lblOrderData.Text = "text";

        //if( seller.Checked ) {
        //      acctType = "Sellers";
        //} else {
        //      acctType = "Buyers";
        //}

        strConnect = @"Provider=Microsoft.ACE.OLEDB.12.0;";
        strConnect += @"Data Source=" + Server.MapPath("App_Data/BayouFarmersDB.accdb") + ";";
        strConnect += @"Persist Security Info=False";

        strCommand = "SELECT * from Users where ID='" + txtUserName.Text + "' AND password='" + password.Text + "'";

        objConnection = new OleDbConnection(strConnect);
        objCommand = new OleDbDataAdapter(strCommand, objConnection);
        dt = new DataTable();
        objCommand.Fill(dt);

        if (dt.Rows.Count == 0)
            lblOrderData.Text = "Login Failed";
        else
        {
            lblOrderData.Text = "Success, Redirecting....";
            acctType = dt.Rows[0]["Type"].ToString();
            Session["AccountType"] = acctType;
            //lblOrderData.Text = "<table cellpadding=\"3\" cellspacing=\"2\">";
            //lblOrderData.Text += "<tr><td>User Name:</td><td>" + dt.Rows[0]["ID"] + "</td>";
            //lblOrderData.Text += "<tr><td>Name:</td><td>" + dt.Rows[0]["firstName"] + " " + dt.Rows[0]["lastName"] + "</td>";
            //lblOrderData.Text += "<tr><td>Street Address:</td><td>" + dt.Rows[0]["addressNumber"] + " <br>" + dt.Rows[0]["Street"] + "</td>";
            //lblOrderData.Text += "<tr><td>City:</td><td>" + dt.Rows[0]["City"] + "</td>";
            //lblOrderData.Text += "<tr><td>State:</td><td>" + dt.Rows[0]["State"] + "</td>";
            //lblOrderData.Text += "<tr><td>Zip:</td><td>" + dt.Rows[0]["Zip"] + "</td>";
            //lblOrderData.Text += "<tr><td>Transaction Date:</td><td>" + Convert.ToDateTime(dt.Rows[0]["TransactionDate"]).ToString("D") + "</td>";
            //lblOrderData.Text += "</table>";
            //lblOrderData.Text += "<br/>";

            //Save the user id to the Session
            Session["userID"] =  txtUserName.Text;

            // Goto default page unless a different path is requested
            string path = "Default.aspx";
            if (Request.Params["returnTo"] != null)
            {
                path = Request.Params["returnTo"];
            }

            Response.Redirect(path);
        }
    }
}
